package ca.etsmtl.log430.lab3;

/**
 * This class defines the Course object for the system.
 * 
 * @author A.J. Lattanze, CMU
 * @version 1.2, 2011-Feb-24
 */

/*
 * Modification Log **********************************************************
 * 
 * v1.2, R. Champagne, 2011-Feb-24 - Various refactorings, conversion of
 * comments to javadoc format.
 * 
 * v1.1, R. Champagne, 2002-Jun-19 - Adapted for use at ETS.
 * 
 * v1.0, A.J. Lattanze, 12/29/99 - Original version.
 * ***************************************************************************
 */

public class Course {
	/** Course title */
	private String courseName;

	/** Course number */
	private String courseID;

	/** Course section (group) */
	private String section;

	/** Days the course will be held on */
	private String days;

	/** Start time of the course */
	private String startTime;

	/** Stop time of the course */
	private String stopTime;

	/** Instructor Name */
	private String instructor;

	/** List of students registered for the course */
	private StudentList registered = new StudentList();

	public Course() {
		this(null);
	}

	public Course(String courseID) {
		this.courseID = courseID;
		this.courseName = null;
		this.section = null;
		this.days = null;
		this.startTime = null;
		this.stopTime = null;
		this.instructor = null;
	}

	/**
	 * Register a student for a class.
	 * 
	 * @param studentObject
	 * @return true is registration is successful, false otherwise.
	 */
	public boolean registerStudent(Student studentObject) {

		if (registered.isStudentInList(studentObject)) {
			return (false);
		} else {
			registered.addStudent(studentObject);
			return (true);
		}
	}

	/**
	 * Register a student for a class.
	 * 
	 * @param studentObject
	 */
	public void register(Student studentObject) {
		registered.addStudent(studentObject);
	}

	/**
	 * @return The list of students registered for the course.
	 */
	public StudentList getRegistered() {
		return registered;
	}

	public void setCourseName(String courseName) {
		this.courseName = courseName;
	}

	public void setCourseID(String courseID) {
		this.courseID = courseID;
	}

	public void setSection(String section) {
		this.section = section;
	}

	public void setDays(String days) {
		this.days = days;
	}

	public void setStartTime(String startTime) {
		this.startTime = startTime;
	}

	public void setStopTime(String stopTime) {
		this.stopTime = stopTime;
	}

	public void setInstructor(String instructor) {
		this.instructor = instructor;
	}

	public String getCourseName() {
		return courseName;
	}

	public String getCourseID() {
		return courseID;
	}

	public String getSection() {
		return section;
	}

	public String getDays() {
		return days;
	}

	public String getStartTime() {
		return startTime;
	}

	public String getStopTime() {
		return stopTime;
	}

	public String getInstructor() {
		return instructor;
	}

	public boolean isStudentAlreadyRegistered(Student currentStudent,
			CourseList ListCourses) {

		ListCourses.goToFrontOfList();
		boolean DoneCourses = false;
		Course courseObject;

		while (!DoneCourses) {
			courseObject = ListCourses.getNextCourse();
			if (courseObject == null) {
				DoneCourses = true;
			} else {
				if (courseObject.courseID.equals(getCourseID())) {

					courseObject.getRegistered().goToFrontOfList();
					boolean DoneStudent = false;
					Student studentObject;

					while (!DoneStudent) {
						studentObject = courseObject.getRegistered()
								.getNextStudent();
						if (studentObject == null) {
							DoneStudent = true;
						} else {
							if (studentObject.getStudentID().equals(
									currentStudent.getStudentID()))
								return true;
						} // if DoneStudent
					} // while DoneStudent
				}

			} // if DoneCourses
		} // while DoneCourses

		return false;
	}

	public Course getAvailableGroup(Student currentStudent,
			CourseList ListCourses) {
		ListCourses.goToFrontOfList();
		boolean DoneCourses = false;
		Course courseObject;

		while (!DoneCourses) {
			courseObject = ListCourses.getNextCourse();
			if (courseObject == null) {
				DoneCourses = true;
			} else {
				if (courseObject.courseID.equals(getCourseID())) {

					if (!currentStudent.isScheduleConflict(courseObject)) {
						return courseObject;
					}
				}

			} // if DoneCourses
		} // while DoneCourses

		return null;

	}

}